/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.srf.classifier;

import edu.yonsei.iwb.sdt.refinement.generator.*;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import java.util.ArrayList;
import java.util.Vector;
import edu.yonsei.iwb.sdt.datastructure.DecomposeCandidate;
import edu.yonsei.iwb.sdt.ontology.PropertyArc;
import edu.yonsei.iwb.sdt.refinement.DLConstructors;
import edu.yonsei.iwb.sdt.refinement.ExpandingHistory;
import edu.yonsei.iwb.sdt.refinement.ExpandingHistorySet;
import edu.yonsei.iwb.sdt.refinement.Refinement;
import edu.yonsei.iwb.ts.TSSearch;
import edu.yonsei.iwb.ts.model.ObjectNode;
import edu.yonsei.iwb.ts.model.Triple;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 *
 * @author JDK
 */
public class QuantificationRefinementClassifier {
    
    public Hashtable<String, ArrayList<String>> getPastResAndValueTable(PropertyArc pa) throws IOException {
        String prop = pa.getProperty();
        Hashtable<String, ArrayList<String>> pastrvt = new Hashtable();
        ArrayList<Triple> trset = TSSearch.listTriple(null, prop, null);
        if (pa.getDirection()) {
            for (int i = 0; i < trset.size(); i++) {
                Triple tr = trset.get(i);
                String pastr = tr.getSubject();
                String value = tr.getObject().getValue();
                if (pastrvt.containsKey(pastr)) {
                    ArrayList<String> valueset = pastrvt.get(pastr);
                    if (!valueset.contains(value)) {
                        valueset.add(value);
                    }
                } else {
                    ArrayList<String> valueset = new ArrayList();
                    valueset.add(value);
                    pastrvt.put(pastr, valueset);
                }
            }
        } else {
            for (int i = 0; i < trset.size(); i++) {
                Triple tr = trset.get(i);
                String value = tr.getSubject();
                String pastr = tr.getObject().getValue();
                if (pastrvt.containsKey(pastr)) {
                    ArrayList<String> pastrSet = pastrvt.get(pastr);
                    if (!pastrSet.contains(value)) {
                        pastrSet.add(value);
                    }
                } else {
                    ArrayList<String> valueset = new ArrayList();
                    valueset.add(value);
                    pastrvt.put(pastr, valueset);
                }
            }
        }
        return pastrvt;
    }

    public Vector<String> getExistentialResources(Vector<String> instanceSet, PropertyArc pa, Hashtable<String, ArrayList<String>> pastrvt) throws IOException {
        Vector<String> resultset = new Vector();        
        
        Enumeration<String> pastrset = pastrvt.keys();
        while (pastrset.hasMoreElements()) {
            boolean exist = false;
            String pastr = pastrset.nextElement();
            ArrayList<String> valueset = pastrvt.get(pastr);
            for (int i = 0; i < valueset.size(); i++) {
                String value = valueset.get(i);
                if (instanceSet.contains(value)) {
                    exist = true;
                    break;
                }
            }

            if (exist) {
                resultset.add(pastr);
            }

        }

        return resultset;
    }

    public Vector<String> getUniversalResources(Vector<String> instanceSet, PropertyArc pa, Hashtable<String, ArrayList<String>> pastrvt) throws IOException {
        Vector<String> resultset = new Vector();        
        Enumeration<String> pastrset = pastrvt.keys();
        while (pastrset.hasMoreElements()) {
            boolean universal = true;
            String pastr = pastrset.nextElement();
            ArrayList<String> valueset = pastrvt.get(pastr);
            for (int i = 0; i < valueset.size(); i++) {
                String value = valueset.get(i);
                if (!instanceSet.contains(value)) {
                    universal = false;
                    break;
                }
            }

            if (universal) {
                resultset.add(pastr);
            }

        }

        return resultset;
    }

    
}
